package com.azhon.appupdate.utils;

import com.azhon.appupdate.config.UpdateConfiguration;
import com.azhon.appupdate.manager.DownloadManager;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import ohos.event.intentagent.IntentAgent;
import ohos.event.intentagent.IntentAgentConstant;
import ohos.event.intentagent.IntentAgentHelper;
import ohos.event.intentagent.IntentAgentInfo;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationSlot;
import ohos.media.image.PixelMap;
import ohos.rpc.RemoteException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目名:    AppUpdate
 * 包名       com.azhon.appupdate.utils
 * 文件名:    NotificationUtil
 * 创建时间:  2018/1/26 on 23:35
 * 描述:     TODO 通知工具类
 *
 * @author 阿钟
 */

public final class NotificationUtil {

    /**
     * 构建一个消息
     *
     * @param title   标题
     * @param content 内容
     */
    private static NotificationRequest builderNotification(String title, String content, PixelMap pixelMap) {
        NotificationSlot slot = getNotificationSlot(); // 创建notificationSlot对象
        try {
            NotificationHelper.addNotificationSlot(slot);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        NotificationRequest request = new NotificationRequest(requireManagerNotNull().getNotifyId());
        request.setSlotId(slot.getId());
        NotificationRequest.NotificationNormalContent contents = new NotificationRequest.NotificationNormalContent();
        contents.setTitle(title)
                .setText(content);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(contents);
        request.setContent(notificationContent).setLittleIcon(pixelMap); // 设置通知的内容
        return request;
    }

    /**
     * 显示刚开始下载的通知
     *
     * @param title   标题
     * @param content 内容
     */
    public static void showNotification(String title, String content, PixelMap pixelMap) {
        NotificationRequest request = builderNotification(title, content, pixelMap);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示正在下载的通知
     *
     * @param title   标题
     * @param content 内容
     */
    public static void showProgressNotification(String title, String content, int progress, int max ,PixelMap pixelMap) {
        NotificationRequest request = builderNotification(title, content,pixelMap);
        request.setProgressBar(progress, max, false);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示下载完成的通知,点击进行安装
     *
     * @param title       标题
     * @param content     内容
     * @param authorities 授权
     * @param apk         安装包
     */
//    TODO 安装功能未实现
    public static void showDoneNotification(String title, String content,
                                            String authorities, File apk) {
        try {
            NotificationHelper.cancelNotification(requireManagerNotNull().getNotifyId());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示下载错误的通知,点击继续下载
     *
     * @param context 上下文
     * @param title   标题
     * @param content 内容
     */
    public static void showErrorNotification(Context context, String title, String content, PixelMap pixelMap) {
        NotificationRequest request = builderNotification(title, content, pixelMap);
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.azhon.appupdate_ohos")
                .withAbilityName("com.azhon.appupdate.service.DownloadService")
                .build();
        intent.setOperation(operation);
        List<Intent> intents = new ArrayList<>();
        intents.add(intent);
        final int requestCode = 200;
        List<IntentAgentConstant.Flags> flags = new ArrayList<>();
        flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);
        IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode, IntentAgentConstant.OperationType.START_SERVICE, flags, intents, null);
        IntentAgent pi = IntentAgentHelper.getIntentAgent(context, paramsInfo);
        request.setIntentAgent(pi);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消通知
     */
    public static void cancelNotification() {
        try {
            NotificationHelper.cancelNotification(requireManagerNotNull().getNotifyId());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取通知栏开关状态
     *
     * @return true |false
     */
    public static boolean notificationEnable() throws RemoteException {
        return NotificationHelper.isAllowedNotify();
    }

    private static UpdateConfiguration requireManagerNotNull() {
        if (DownloadManager.getInstance() == null) {
            return new UpdateConfiguration();
        }
        return DownloadManager.getInstance().getConfiguration();
    }

    private static NotificationSlot getNotificationSlot() {
        UpdateConfiguration config = requireManagerNotNull();
        NotificationSlot channel = config.getNotificationChannel();
        //如果用户没有设置
        if (channel == null) {
            channel = new NotificationSlot(Constant.DEFAULT_CHANNEL_ID, Constant.DEFAULT_CHANNEL_NAME, NotificationSlot.LEVEL_MIN);
        }
        return channel;
    }
}
